package ua.ck.example.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;

import ua.ck.example.domain.Customer;
import ua.ck.example.domain.Employee;
import ua.ck.example.domain.Provider;
import ua.ck.example.service.CustomerService;
import ua.ck.example.service.EmployeeService;
import ua.ck.example.service.ProviderService;
import ua.ck.example.util.Constant;
import ua.ck.example.web.dto.UserDto;

@Controller
public class UserController {

	@Autowired
	private ProviderService providerService;
	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private CustomerService customerService;

	// http://localhost:8080/e-shop-example/provider/register
	@RequestMapping(value = "/provider/register", method = RequestMethod.GET)
	public String providerRegisterForm(WebRequest request, Model model) {
		Provider providerDto = new Provider();
		model.addAttribute("providerDto", providerDto);
		return "provider/register";
	}

	@RequestMapping(value = "/provider/register", method = RequestMethod.POST)
	public String providerRegister(
			@ModelAttribute("providerDto") @Valid Provider providerDto,
			BindingResult result, WebRequest request, Errors errors,
			HttpServletRequest req) {
		System.out.println("c");
		if (!result.hasErrors()) {
			providerService.addProvider(providerDto);
			HttpSession httpSession = req.getSession();
			httpSession.setAttribute(Constant.SESSION_KEY_USER, providerDto);
			httpSession.setAttribute(Constant.SESSION_KEY_USER_TYPE,
					Constant.SESSION_PROVIDER_TYPE);
			return "redirect:/product/index";
		} else {
			return "provider/register";
		}
	}

	@RequestMapping(value = "/employee/register", method = RequestMethod.GET)
	public String employeeRegisterForm(WebRequest request, Model model) {
		Employee employeeDto = new Employee();
		model.addAttribute("employeeDto", employeeDto);
		return "employee/register";
	}

	@RequestMapping(value = "/employee/register", method = RequestMethod.POST)
	public String employeeRegister(
			@ModelAttribute("employeeDto") @Valid Employee employeeDto,
			BindingResult result, WebRequest request, Errors errors,
			HttpServletRequest req) {
		System.out.println("c");
		if (!result.hasErrors()) {
			employeeService.addEmployee(employeeDto);
			HttpSession httpSession = req.getSession();
			httpSession.setAttribute(Constant.SESSION_KEY_USER, employeeDto);
			httpSession.setAttribute(Constant.SESSION_KEY_USER_TYPE,
					Constant.SESSION_EMPLOYEE_TYPE);
			return "redirect:/product/index";
		} else {
			return "employee/register";
		}
	}

	@RequestMapping(value = "/customer/register", method = RequestMethod.GET)
	public String customerRegisterForm(WebRequest request, Model model) {
		Customer customerDto = new Customer();
		model.addAttribute("customerDto", customerDto);
		return "customer/register";
	}

	@RequestMapping(value = "/customer/register", method = RequestMethod.POST)
	public String customerRegister(
			@ModelAttribute("customerDto") @Valid Customer customerDto,
			BindingResult result, WebRequest request, Errors errors,
			HttpServletRequest req) {
		System.out.println("c");
		if (!result.hasErrors()) {
			customerService.addCustomer(customerDto);
			HttpSession httpSession = req.getSession();
			httpSession.setAttribute(Constant.SESSION_KEY_USER, customerDto);
			httpSession.setAttribute(Constant.SESSION_KEY_USER_TYPE,
					Constant.SESSION_CUSTOMER_TYPE);
			return "redirect:/product/index";
		} else {
			return "customer/register";
		}
	}

	// http://localhost:8080/e-shop-example/login
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login(WebRequest request, Model model, HttpServletRequest req) {
		if (req.getSession().getAttribute(Constant.SESSION_KEY_USER) != null) {
			return "redirect:/product/index";
		}
		UserDto userDto = new UserDto();
		model.addAttribute("userDto", userDto);
		return "login";
	}

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(@ModelAttribute("userDto") @Valid UserDto userDto,
			BindingResult result, WebRequest request, Errors errors,
			HttpServletRequest req) {

		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String type = req.getParameter("type");
		HttpSession httpSession = req.getSession();
		if (type.equals(Constant.SESSION_PROVIDER_TYPE)) {
			Provider providerParam = new Provider();
			providerParam.setProviderName(username);
			providerParam.setPassword(password);
			Provider providerGet = providerService.getProvider(providerParam);
			if (providerGet != null) {
				httpSession
						.setAttribute(Constant.SESSION_KEY_USER, providerGet);
				httpSession.setAttribute(Constant.SESSION_KEY_USER_TYPE,
						Constant.SESSION_PROVIDER_TYPE);
				return "redirect:/product/index";
			}

		} else if (type.equals(Constant.SESSION_CUSTOMER_TYPE)) {
			Customer customerParam = new Customer();
			customerParam.setCustomerName(username);
			customerParam.setPassword(password);
			Customer customerGet = customerService.getCustomer(customerParam);
			if (customerGet != null) {
				httpSession
						.setAttribute(Constant.SESSION_KEY_USER, customerGet);
				httpSession.setAttribute(Constant.SESSION_KEY_USER_TYPE,
						Constant.SESSION_CUSTOMER_TYPE);
				return "redirect:/product/index";
			}
		} else if (type.equals(Constant.SESSION_EMPLOYEE_TYPE)) {
			Employee employeeParam = new Employee();
			employeeParam.setEmpName(username);
			employeeParam.setPassword(password);
			Employee employeeGet = employeeService.getEmployee(employeeParam);
			if (employeeGet != null) {
				httpSession
						.setAttribute(Constant.SESSION_KEY_USER, employeeGet);
				httpSession.setAttribute(Constant.SESSION_KEY_USER_TYPE,
						Constant.SESSION_EMPLOYEE_TYPE);
				return "redirect:/product/index";
			}

		}
		return "login";
	}

	@RequestMapping("/logout")
	public String loginOut(HttpServletRequest req) {
		req.getSession().removeAttribute(Constant.SESSION_KEY_USER);
		req.getSession().invalidate();
		return "redirect:/login";
	}

}
